<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="88px">
      <el-form-item label="交易流水号" prop="jylsh">
                <el-input
                  v-model="queryParams.jylsh"
                  placeholder="请输入订单号（61开头）"
                  clearable
                  @keyup.enter.native="handleQuery"
                />
<!--        <el-select v-model="queryParams.gfmc" filterable remote placeholder="请选择" :remote-method="queryGfmc">-->
<!--          <el-option-->
<!--            v-for="item in gfmcOptions"-->
<!--            :key="item"-->
<!--            :label="item"-->
<!--            :value="item"-->
<!--          ></el-option>-->
<!--        </el-select>-->
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <span style="color: red; margin-left: 50px; font-size: medium ">如果未查询到数据，可能是发票尚未导入系统，请于支付完成3日后查询。咨询发票相关问题，可联系fapiao@bjzkly.cn</span>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="fpxxList">
      <el-table-column label="序号" type="index" width="50"></el-table-column>
      <el-table-column label="发票号码" align="center" prop="fphm" width="200"/>
      <el-table-column label="单位名称" align="left" prop="gfmc" width="350"/>
      <el-table-column label="附件名称" align="left" prop="fjmc"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="200">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-download"
            @click="handleExport(scope.row,'pdf')"
          >pdf下载
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-download"
            @click="handleExport(scope.row,'ofd')"
          >ofd下载
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-download"
            @click="handleExport(scope.row,'xml')"
          >xml下载
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import {queryByGfmc, queryGfmc} from "@/api/ca/fpxx";
import axios from 'axios';
import {Message} from 'element-ui'

export default {
  name: "Fpxx",
  data() {
    return {
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 发票信息表格数据
      fpxxList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        jylsh: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      gfmcOptions: []
    };
  },
  created() {

  },
  methods: {
    queryGfmc(gfmc) {
      queryGfmc({"gfmc":gfmc}).then(response => {
        this.gfmcOptions = response;
      });
    },
    /** 查询发票信息列表 */
    getList() {
      this.loading = true;
      queryByGfmc(this.queryParams).then(response => {
        if (response.msg === "查询成功") {
          this.fpxxList = response.rows;
          this.total = response.total;
        } else {
          Message({message: response.msg, type: 'error'})
        }
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 下载 */
    async handleExport(row, suffix) {
      let fileName = row.fjmc + "." + suffix;

      const url = process.env.VUE_APP_BASE_API + `/ca/fpxx/downLoad?fileName=${encodeURIComponent(fileName)}`;
      const response = await axios({
        method: 'get',
        url,
        responseType: 'blob', // Important for handling binary data
      });

      // Create a URL object for the blob data
      const downloadUrl = window.URL.createObjectURL(new Blob([response.data]));

      // Create a hidden anchor element and simulate a click to trigger the download
      const a = document.createElement('a');
      a.style.display = 'none';
      a.href = downloadUrl;
      a.download = fileName;
      document.body.appendChild(a);
      a.click();

      // Clean up
      window.URL.revokeObjectURL(downloadUrl);
      document.body.removeChild(a);
    }
  }
};
</script>
